package com.yvan.demo.platform;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yvan.PageDb;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("Rest返回对象")
public class Model<T> {

    public static <T> Model<T> newSuccess(T data) {
        return new Model<T>().setData(data).setSuccess(true);
    }

    public static <T> Model<T> newSuccess(PageDb pagination, T data) {
        return new Model<T>().setData(data).setSuccess(true).setPagination(pagination);
    }

    public static <T> Model<T> newFail(String msg) {
        return new Model<T>().setMsg(msg).setSuccess(false);
    }

    @ApiModelProperty(value = "业务是否成功")
    private boolean success = false;

    @ApiModelProperty(value = "消息提示")
    private String msg = "";

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "返回数据")
    private T data;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "分页属性")
    private PageDb pagination;

    public boolean isSuccess() {
        return success;
    }

    public Model<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Model<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public Model<T> setData(T data) {
        this.data = data;
        return this;
    }

    public PageDb getPagination() {
        return pagination;
    }

    public Model<T> setPagination(PageDb pagination) {
        this.pagination = pagination;
        return this;
    }
}
